/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-14 19:47:12
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-29 21:01:32
 * @FilePath: \郭招娣\hou\app\controller\articleLists.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 22:12:08
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken')
const axios = require('axios')
const client_id= '23f85b43816b15daeee77c54e92f1e7c139109fcf15f5c7f970c724745e4eb19';
const redirect_uri= 'http://localhost:7001/api/auth';
const client_secret ='6ea50ae176fb8af299a98d9aeb1fe08916ba4f3ecbd204634178d337744541ac';
class ArticleListController extends Controller {
    // 第三方登录
    async san(){
        const { ctx }=this;
        const { code } = ctx.query;
        const res = await axios.post(`https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${client_id}&redirect_uri=${redirect_uri}&client_secret=${client_secret}`)
        const access_token = res.data.access_token;
        const userInfo = await axios.get(`https://gitee.com/api/v5/user?access_token=${access_token}`)
        ctx.response.redirect(`http://localhost:8080/article/?name=${userInfo.data.name}&url=${userInfo.data.url}`)
    }
    // 获取文章列表
    async articleImg() {
        const { ctx } = this;
        const { status } = ctx.request.body;
        let list = await this.app.mysql.select('articleLists', { status });
        if (list) {
            ctx.body = {
                code: 200,
                msg: '成功',
                list,
                total: list.length
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }
    // 标签
    async tagLists() {
        const { ctx } = this;
        const tagList = await this.app.mysql.select('tagLists');
        if (tagList) {
            ctx.body = {
                code: 200,
                msg: '获取成功',
                list: tagList,
                total: tagList.length
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '获取失败',
            }
        }
    }
    // 查询分类
    async classLists() {
        const { ctx } = this;
        const list = await this.app.mysql.select('classLists');
        if (list) {
            ctx.body = {
                code: 200,
                msg: '获取成功',
                list,
                total: list.length
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '获取失败',
            }
        }
    }
    // 搜索
    async search() {
        const { ctx } = this;
        const { name } = ctx.request.body;
        // const results = await this.app.mysql.query(`select * from articleLists where title like '%${val}%'`);
        // if(results){
        //     ctx.body={
        //         code:200,
        //         msg:'搜索成功',
        //         list: results

        //     }
        // }else{
        //     ctx.body = {
        //         code: 500,
        //         msg: '搜索失败'
        //     }
        // }
        const post = await this.app.mysql.get('searchLists', { name });

        if (post) {
            const row = {
                num: post.num + 1
            };

            const options = {
                where: {
                    name
                }
            };
            const result = await this.app.mysql.update('searchLists', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    result,
                    msg: '搜索成功'
                }
            }
        } else {
            const result = await this.app.mysql.insert('searchLists', { name, date: new Date(), num: 1 });
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '搜索成功添加'
                }
            }
        }

    }

    // 点赞
    async zan() {
        const { ctx } = this;
        let { state, id, likes } = ctx.request.body;
        if (state) {
            const row = {
                id,
                likes: likes * 1 + 1
            };
            const result = await this.app.mysql.update('articleLists', row);
            if (result.affectedRows === 1) {
                ctx.body = {
                    code: 200,
                    msg: '点赞成功'
                }
            }
        } else {
            const row = {
                id,
                likes: likes * 1 - 1
            };
            const result = await this.app.mysql.update('articleLists', row);
            if (result.affectedRows === 1) {
                ctx.body = {
                    code: 200,
                    msg: '点赞失败'
                }
            }
        }
    }

    // 知识小册
    async knowledge() {
        const { ctx } = this;
        const tagList = await this.app.mysql.select('knowledgeLists');
        if (tagList) {
            ctx.body = {
                code: 200,
                msg: '获取成功',
                list: tagList,
                total: tagList.length
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '获取失败',
            }
        }
    }

    // 登录
    async login() {
        const { ctx } = this;
        const { username, password, email } = ctx.request.body;
        const post = await this.app.mysql.get('user', { username });
        if (post) {
            const token = jwt.sign({ username, password, email },'gzd');
            ctx.body = {
                list: post,
                username,
                mag: '登录成功',
                code:200
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '登录失败'
            }
        }
    }
}

module.exports = ArticleListController;
